package com.pengtu.service.project;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.project.DrawingsContractInfoDao;
import com.pengtu.dao.project.ProgressReportDao;
import com.pengtu.dao.project.ProjectInfoDao;
import com.pengtu.dao.project.SignUpContractDao;
import com.pengtu.entity.project.DrawingsContractInfo;
import com.pengtu.entity.project.ProgressReport;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.project.SignUpContract;
import com.pengtu.entity.project.SubentryInfo;
import com.pengtu.entity.project.SubprojectInfo;
import com.pengtu.entity.project.SubunitInfo;
import com.pengtu.model.LuceneProjectModel;

@Component
@Transactional
public class ProjectManager {
	private ProjectInfoDao projectDao;
	private SignUpContractDao signUpContractDao;
	private DrawingsContractInfoDao drawingsContractInfoDao;
	private SubManager subManager;
	private ProgressReportDao progressReportDao;

	/******* 操作Proj对象 **********/
	/**
	 * 保存或修改proj对象
	 * 
	 * @param proj
	 */
	public void saveOrupdateProj(ProjectInfo proj) {
		projectDao.save(proj);
	}

	/**
	 * 删除标识为id的对象
	 * 
	 * @param id
	 */
	public void deleteProj(String id) {
		projectDao.delete(id);
	}

	/**
	 * 根据属性名称获取单条数据
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	@Transactional(readOnly = true)
	public ProjectInfo getProj(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id"))
			return projectDao.get(value);
		return projectDao.findUniqueBy(propertyName, value);
	}

	public int findAllproj() {
		// TODO Auto-generated method stub
		return projectDao.find().size();
	}

	/**
	 * 根据条件分页获取记录
	 * 
	 * @param page
	 * @param queryfilter
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<ProjectInfo> getPageProj(final Page<ProjectInfo> page,
			final QueryFilter queryfilter) {
		return projectDao.findPage(page, queryfilter);
	}

	@Transactional(readOnly = true)
	public List<ProjectInfo> findListProj(QueryFilter queryfilter) {
		// TODO Auto-generated method stub
		return projectDao.find(queryfilter);
	}

	public List<ProjectInfo> getPageProj() {
		return projectDao.getAll();
	}

	/************** 操作SignUpContract对象 ********************/
	/**
	 * 保存或修改
	 */
	public void saveOrupdateSign(SignUpContract sign) {
		signUpContractDao.save(sign);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 */
	public void deleteSign(String id) {
		signUpContractDao.delete(id);
	}

	public boolean isContractOrderUnique(String oldContractOrder,
			String newContractOrder) {
		// TODO Auto-generated method stub
		return signUpContractDao.isPropertyUnique("contractOrder",
				oldContractOrder, newContractOrder);
	}

	public boolean isContractNumUnique(String oldContractNum,
			String newContractNum) {
		// TODO Auto-generated method stub
		return signUpContractDao.isPropertyUnique("contractNum",
				oldContractNum, newContractNum);
	}

	/**
	 * 根据属性获取单条数据
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	@Transactional(readOnly = true)
	public SignUpContract getSign(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id"))
			return signUpContractDao.get(value);
		return signUpContractDao.findUniqueBy(propertyName, value);
	}

	/**
	 * 
	 * findPageSign: 适用: 根据多条属性获取单条数据
	 * 
	 * @param queryFilter
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	@Transactional(readOnly = true)
	public List<SignUpContract> findPageSign(QueryFilter queryFilter) {
		// TODO Auto-generated method stub
		return signUpContractDao.find(queryFilter);
	}

	/**
	 * 根据项目和合同段类型查询合同段 getSignUpContracts: 适用:
	 * 
	 * @param projectInfo
	 * @param contractKind
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	@Transactional(readOnly = true)
	public List<SignUpContract> getSignUpContracts(String projectId,
			String contractKind) {
		String hql = "from SignUpContract suc where suc.projectId = ? and suc.contractKind = ?";
		return signUpContractDao.find(hql, projectId, contractKind);
	}

	public List<SignUpContract> getSignUpContracts(String projectId) {
		return this.getSignUpContracts(projectId, "4");
	}

	/**
	 * 查询
	 * 
	 * @param propertyName
	 * @param value
	 * @return
	 */
	@Transactional(readOnly=true)
	public List<SignUpContract> findSignList(String propertyName, String value) {
		return signUpContractDao.findBy(propertyName, value);
	}
	@Transactional(readOnly=true)
	public List<SignUpContract> findSignQueryList(QueryFilter signfilter) {
		// TODO Auto-generated method stub
		return signUpContractDao.find(signfilter);
	}
	
	/**
	 * 分页获取对象
	 * 
	 * @param page
	 * @param queryfilter
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<SignUpContract> findPageSign(final Page<SignUpContract> page,
			final QueryFilter queryfilter) {
		return signUpContractDao.findPage(page, queryfilter);
	}

	public List<SignUpContract> findSignAll(String projectId, String value) {
		String hql = "select su from SignUpContract as su where su.projectId=? and su.contractKind=?";
		Object[] values = { projectId, value };
		return signUpContractDao.find(hql, values);
	}

	// 操作DrawingsContractInfo对象
	/**
	 * 按照属性查找单条信息
	 */
	@Transactional(readOnly = true)
	public DrawingsContractInfo getDrowConract(String propertyName, String value) {
		// TODO Auto-generated method stub
		if (propertyName.equalsIgnoreCase("id"))
			return drawingsContractInfoDao.get(value);
		return drawingsContractInfoDao.findUniqueBy(propertyName, value);
	}

	/**
	 * 按照属性查找集合对象
	 * 
	 * @param projectId
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<DrawingsContractInfo> findDrawContractList(String projectId) {
		// TODO Auto-generated method stub
		return drawingsContractInfoDao.findBy("projectId", projectId);
	}

	/**
	 * 保存或修改对象
	 * 
	 * @param drawcontract
	 */
	public void saveOrUpdateDrawContract(DrawingsContractInfo drawcontract) {
		// TODO Auto-generated method stub
		drawingsContractInfoDao.save(drawcontract);
	}

	/**
	 * 删除Id对象
	 * 
	 * @param id
	 */
	public void deleteDrawContract(String id) {
		// TODO Auto-generated method stub
		drawingsContractInfoDao.delete(id);
	}

	/**
	 * 填充TreeGrid
	 * 
	 * @param drawContractList
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Map<String, Object>> fillTreeGrid(
			List<DrawingsContractInfo> drawContractList) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> drawContractSet = new ArrayList<Map<String, Object>>();
		for (DrawingsContractInfo drawContract : drawContractList) {
			Map<String, Object> drawContractMap = null;
			drawContractMap = new HashMap<String, Object>();
			drawContractMap.put("id", "0@" + drawContract.getId());
			drawContractMap.put("name", drawContract.getContractOrder());
			drawContractMap.put("startPegNum", drawContract.getStartPegNum());
			drawContractMap.put("endPegNum", drawContract.getEndPegNum());
			drawContractMap.put("createDate", drawContract.getCreateDate());

			List<SubunitInfo> subunitList = subManager.findBySubunit(
					"drawingsContractId", drawContract.getId());

			List<Map<String, Object>> subunitSet = new ArrayList<Map<String, Object>>();
			for (SubunitInfo subunitInfo : subunitList) {
				Map<String, Object> subunitMap = new HashMap<String, Object>();
				subunitMap.put("id", "1@" + subunitInfo.getId());
				subunitMap.put("name", subunitInfo.getName());
				subunitMap.put("startPegNum", subunitInfo.getStartPegNum());
				subunitMap.put("endPegNum", subunitInfo.getEndPegNum());
				subunitMap.put("createDate", subunitInfo.getCreateDate());

				List<SubprojectInfo> subprojectList = subManager
						.findBySubproject("subunitId", subunitInfo.getId());

				List<Map<String, Object>> subprojectSet = new ArrayList<Map<String, Object>>();
				for (SubprojectInfo subprojectInfo : subprojectList) {
					Map<String, Object> subprojectMap = new HashMap<String, Object>();
					subprojectMap.put("id", "2@" + subprojectInfo.getId());
					subprojectMap.put("name", subprojectInfo.getName());
					subprojectMap.put("startPegNum",
							subprojectInfo.getStartPegNum());
					subprojectMap.put("endPegNum",
							subprojectInfo.getEndPegNum());
					subprojectMap.put("createDate",
							subprojectInfo.getCreateDate());

					List<SubentryInfo> subentryList = subManager
							.findBySubentryInfo("subprojectId",
									subprojectInfo.getId());

					List<Map<String, Object>> subentrySet = new ArrayList<Map<String, Object>>();
					for (SubentryInfo subentryInfo : subentryList) {
						Map<String, Object> subentryMap = new HashMap<String, Object>();
						subentryMap.put("id", "3@" + subentryInfo.getId());
						subentryMap.put("name", subentryInfo.getName());
						subentryMap.put("startPegNum",
								subentryInfo.getStartPegNum());
						subentryMap.put("endPegNum",
								subentryInfo.getEndPegNum());
						subentryMap.put("createDate",
								subentryInfo.getCreateDate());
						subentryMap.put("leaf", Boolean.TRUE);
						subentrySet.add(subentryMap);
					}
					if (subentrySet.size() != 0) {
						subprojectMap.put("children", subentrySet);
					} else {
						subprojectMap.put("leaf", Boolean.TRUE);
					}
					subprojectSet.add(subprojectMap);
				}
				if (subprojectSet.size() != 0) {
					subunitMap.put("children", subprojectSet);
				} else {
					subunitMap.put("leaf", Boolean.TRUE);
				}
				subunitSet.add(subunitMap);
			}
			drawContractMap.put("children", subunitSet);
			drawContractSet.add(drawContractMap);
		}
		return drawContractSet;
	}

	// ************操作progressReport***************************//

	public void saveProGre(ProgressReport progressReptort) {
		progressReportDao.save(progressReptort);
	}

	public void deleteProGre(String id) {
		progressReportDao.delete(id);
	}

	@Transactional(readOnly = true)
	public ProgressReport getByProGre(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id"))
			return progressReportDao.get(value);
		return progressReportDao.findUniqueBy(propertyName, value);
	}

	@Transactional(readOnly = true)
	public Page<ProgressReport> findPageProGre(final Page<ProgressReport> page,
			final QueryFilter queryFilter) {
		return progressReportDao.findPage(page, queryFilter);
	}

	@Autowired
	public void setSubManager(SubManager subManager) {
		this.subManager = subManager;
	}

	@Autowired
	public void setDrawingsContractInfoDao(
			DrawingsContractInfoDao drawingsContractInfoDao) {
		this.drawingsContractInfoDao = drawingsContractInfoDao;
	}

	@Autowired
	public void setSignUpContractDao(SignUpContractDao signUpContractDao) {
		this.signUpContractDao = signUpContractDao;
	}

	@Autowired
	public void setProjectDao(ProjectInfoDao projectDao) {
		this.projectDao = projectDao;
	}

	@Autowired
	public void setProgressReportDao(ProgressReportDao progressReportDao) {
		this.progressReportDao = progressReportDao;
	}

	public void createIndexByLucene(String index) {

		projectDao.createIndexByLucene(index);
	}

	public List<LuceneProjectModel> SearchByLucene(String index,
			String keyword, String startDate, String endDate) {
		return projectDao.SearchByLucene(index, keyword, startDate, endDate);
	}

	/**
	 * 验证工程名是否唯一
	 * 
	 * @param newProjName
	 * @param oldProjeName
	 * @return
	 */
	public boolean checkProjName(String newProjName, String oldProjeName) {
		return projectDao.isPropertyUnique("projName", newProjName,
				oldProjeName);
	}


}
